<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class User extends CI_Controller {
	
	/**
	 * Login the system admin
	 */
	public function login() {
		$this->load->model ( 'Users', null, true );
		$data = array ();
		//post data login
		if ($this->input->post ()) {
			$data = $this->input->post ();
			if ($this->Users->login ( $data ['username'], $data ['password'] )) {
				redirect ( base_url () . 'config.html' );
			} else {
				$this->session->set_flashdata ( 'message', 'Tài khoản hoặc mật khẩu không đúng.' );
			}
		}
		$this->load->view ( 'user/login', $data );
	}
	
	/**
	 * Get index
	 * @author tien.nguyen
	 */
	public function index($start = 0) {
		$this->load->Model ( 'Users', null, true );
		
		$data ['Users'] = $this->Users->getListUsers ( $start );
		$this->load->view ( 'user/index', $data );
	}
	
	/**
	 * edit user
	 * @author tien.nguyen
	 */
	public function edit($userId) {
		$this->load->model ( 'Users', null, true );
		
		if ($this->input->post ()) {
			$data = $this->input->post ();
			if ($this->Users->edit ( $data, $userId )) {
				$this->session->set_flashdata ( 'message', 'Lưu thành công.' );
				redirect ( base_url () . 'user.html' );
			} else {
				$this->session->set_flashdata ( 'message', 'Lưu không thành công.' );
			}
		}
		
		$data ['User'] = $this->Users->getUser ( $userId );
		$this->load->view ( 'user/edit', $data );
	}
	
	/**
	 * edit user
	 * @author tien.nguyen
	 */
	public function add() {
		$this->load->model ( 'Users', null, true );
		$data = array ();
		if ($this->input->post ()) {
			$data = $this->input->post ();
			if ($this->Users->add ( $data )) {
				$this->session->set_flashdata ( 'message', 'Lưu thành công.' );
				redirect ( base_url () . 'user.html' );
			} else {
				$this->session->set_flashdata ( 'message', 'Lưu không thành công.' );
			}
		}
		
		$this->load->view ( 'user/add', $data );
	}
	
	/**
	 * Logout the system
	 * @author tien.nguyen
	 */
	public function logout() {
		$arr = array ('UserId', 'UserName', 'Password', 'Enable', 'CreatedDate', 'Email', 'Phone', 'Address', 'FullName', 'Role' );
		foreach ( $arr as $value ) {
			$this->session->set_userdata ( 'admin_' . $value, null );
		}
		redirect ( base_url () . 'user/login.html' );
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */